package SocketTest;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServerExtend {
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket ss = new ServerSocket(5678);
        Socket sc = ss.accept();

        OutputStream os = sc.getOutputStream();
        InputStream is = sc.getInputStream();
        int counter = 0;

        do {
            String s = "hello,here is server." + counter + "\n";
            os.write(s.getBytes());
            Thread.sleep(1000);
//            System.out.println("write pipe " + counter);

            byte[] temp = new byte[100];
            if(is.available() != 0) {
                int n = is.read(temp);
                System.out.println("message from client:" + new String(temp,0,n));
                if (new String(temp).startsWith("exit"))
                    break;
            }

        } while (counter++ != 100);

        os.close();
    }
}
